//分页
function changePage(){
    var page = $("#pageInput").val();
    if(page == undefined || page == null || page.trim().length <= 0){
        return false;
    }
    var url = $("#pageUrlFormat").val().replace("%d", page - 1);
    window.location.href = url;
}
//页码校验v1
function validatePage(){
    var maxPage = $("#maxPage").val();
    var page = $("#pageInput").val();
    var pattern = /^[1-9]\d{0,2}$/;
    if(page.trim()==""){
        $('#pagebtn').blur();
        return;
    }
    if(pattern.test(page)){
        page = parseInt(page);
        maxPage = parseInt(maxPage);
        if(page > maxPage || page < 0){
            alert("请输入1~" + maxPage + "之间的数字");
            $("#pageInput").val("");
            $('#pagebtn').blur();
            return;
        }
    }else{
        alert("请输入1~" + maxPage + "之间的数字");
        $("#pageInput").val("");
        $('#pagebtn').blur();
        return;
    }
}
//页码校验v2
function validPage(){
    var maxPage = $("#maxPage").val();
    var page = $("#pageInput").val();
    var pattern = /^[0-9]+$/;
    var pageInt = parseInt(page);
    var oldpage = $("#pageInput").attr("oldpage");
    if(page.trim() == ""){
        $("#pageInput").val("");
        return;
    }
    if(!pattern.test(page.replace(oldpage, ''))){
        $("#pageInput").val(oldpage);
        return;
    }
    if(page.substr(0,1) == '0'){
        $("#pageInput").val(parseInt(page));
    }
    if(pageInt == 0){
        $("#pageInput").val('');
        return;
    }
    if(pageInt > parseInt(maxPage)){
        $("#pageInput").val(page.substr(0,page.length-1));
        return;
    }
    $('#pageInput').attr('oldpage', $('#pageInput').val());
}
$(function(){
    $('#pagebtn').blur();
    $('.page #pageInput').keyup(function(e){
        if(e.keyCode==13){
            changePage();
        }
    })
    $('#pageInput').keyup(function(e){
        switch (e.keyCode){
            case 37: break;
            case 38: break;
            case 39: break;
            case 40: break;
            case 8: $('#pageInput').attr('oldpage', $('#pageInput').val()); break;
            case 46: break;
            default :validPage();
        }
    })
})



String.prototype.setQuery = function (name, value) {
    var url = this.toString();
    var params = [], exists = false, urlPrefix = url;
    if (url.indexOf('?') > 0) {
        var search = url.substr(url.indexOf('?') + 1);
        urlPrefix = url.substr(0, url.indexOf('?'));
        var nameValues = search.split('&');
        if (nameValues.length > 0) {
            for (var i = 0; i < nameValues.length; i++) {
                var index = nameValues[i].indexOf('='), n = '', v = '';
                if (index > 0) {
                    n = nameValues[i].substr(0, index);
                    v = nameValues[i].substr(index + 1);
                }
                else {
                    n = nameValues[i];
                }

                if (name == n) {
                    if (value) {
                        params.push(name + '=' + encodeURIComponent(value));
                    }

                    exists = true;
                }
                else {
                    if(n!=null&&n!=""){
                        params.push(n + '=' + (v || ''));
                    }
                }
            }
        }
    }

    var result = url;
    if (exists) {
        if(params.length > 0)
            result = urlPrefix + '?' + params.join('&');
        else
            result = urlPrefix;
    }
    else if (urlPrefix && value) {
        if (value) {
            params.push(name + '=' + encodeURIComponent(value));
            result = urlPrefix + '?' + params.join('&');
        }
    }

    return result;
};

//获取url中的参数
String.prototype.getQuery = function (name) {
    var value = null;
    var url = this.toString();
    if (url.indexOf('?') > -1) {
        var search = url.substr(url.indexOf('?') + 1);
        if (search.indexOf('#') > -1) {
            search = url.substr(0, url.indexOf('#'));
        }

        var nameValues = search.split('&');
        if (nameValues.length > 0) {
            for (var i = 0; i < nameValues.length; i++) {
                if (nameValues[i].indexOf(name + '=') == 0) {
                    value = decodeURIComponent(nameValues[i].substr(name.length + 1));
                    break;
                }

                if (nameValues[i] == name) {
                    value = '';
                    break;
                }
            }
        }
    }
    if(value)
        value = value.replace("\r", "").replace("\n", "").replace(">", " ").replace("<", " ").replace("\"", " ").replace("'", " ");
    return value;
}
HT = {};
HT.utils = {};
HT.utils.pagination = function(maxPage, pageInput, pageButton){
    pageInput.keyup(function(e){
        switch (e.keyCode){
            case 37: break;
            case 38: break;
            case 39: break;
            case 40: break;
            case 13:
                changePage();
                break;
            case 8: $(e.target).attr('oldpage', $(e.target).val()); break;
            case 46: $(e.target).attr('oldpage', $(e.target).val()); break;
            default :validPage();
        }
    }).focus(function(){
        $(this).toggleClass('active');
    }).blur(function(){
        $(this).toggleClass('active');
    });

    pageButton.click(function(){
        changePage();
    });

    //分页
    function changePage(){
        var page = pageInput.val();
        var pattern = /^[1-9]\d{0,2}$/;
        if(page.trim()==""){
            pageInput.focus();
            return;
        }
        if(pattern.test(page)){
            page = parseInt(page);
            maxPage = parseInt(maxPage);
            if(page > maxPage || page < 0){
                alert("请输入1~" + maxPage + "之间的数字");
                pageInput.val("").focus();
                return;
            }
        }else{
            alert("请输入1~" + maxPage + "之间的数字");
            pageInput.val("").focus();
            return;
        }

        var page = parseInt(pageInput.val()) || 1;
        var url = document.location.href.setQuery('page', page - 1);
        window.location.href = url;
    }
    //页码校验v2
    function validPage(){
        var page = pageInput.val();
        var pattern = /^[0-9]+$/;
        var oldpage = pageInput.attr("oldpage") || '';
        if(page.trim() == ""){
            pageInput.val("");
            return;
        }
        if(!pattern.test(page)){
            pageInput.val(oldpage);
            return;
        }
        var pageInt = parseInt(page);
        if(page.substr(0,1) == '0'){
            pageInput.val(pageInt);
        }
        if(pageInt == 0){
            pageInput.val('');
            return;
        }
        if(pageInt > parseInt(maxPage)){
            pageInput.val(page.substr(0,page.length-1));
            return;
        }
        pageInput.attr('oldpage', pageInput.val());
    }
};

HT.utils.setCookie = function (name, value, expireMins, domain) {
    var d = new Date();
    d.setTime(d.getTime() + ((expireMins || 60 * 24) * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    var domainStr = "";
    if(domain && domain.length > 0)
        domainStr = ";domain=" + domain;
    document.cookie = name + "=" + value + ";" + expires + ";path=/" + domainStr;
}

HT.utils.getCookie = function (key) {
    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
    return keyValue ? keyValue[2] : null;
}